In [4]:
import graphlab as gl
In [7]:
image_train = gl.SFrame('image_train_data/')
In [5]:
knn_model = gl.nearest_neighbors.create(image_train, features=['deep_features'],
label = 'id')
In [13]:
gl.canvas.set_target('ipynb')
cat = image_train[18:19]
cat['image'].show()
In [8]:
knn_model.query(cat)
Out[8]:
In [11]:
def get_images_from_ids(query_result):
return image_train.filter_by(query_result['reference_label'], 'id')
In [14]:
cat_neighbors = get_images_from_ids(knn_model.query(cat))
In [19]:
cat_neighbors['image'].show()
In [20]:
car = image_train[8:9]
In [21]:
car['image'].show()
In [22]:
get_images_from_ids(knn_model.query(car))['image'].show()
In [23]:
show_neighbors = lambda i:get_images_from_ids(knn_model.query(image_train[i:i+1]))['image'].show()
In [26]:
show_neighbors(19)
In [28]:
show_neighbors(1222)
In [30]:
show_neighbors(2000)
In [33]:
image_train['label'].sketch_summary()
Out[33]:
In [ ]:
In [35]:
dog_image_train = image_train[image_train['label']=='dog']
In [69]:
cat_image_train = image_train[image_train['label']=='cat']
##cat_image_train.head()
In [70]:
auto_image_train = image_train[image_train['label']=='automobile']
In [71]:
bird_image_train = image_train[image_train['label']=='bird']
In [41]:
dog_model = gl.nearest_neighbors.create(dog_image_train,
features = ['deep_features'],
label = 'id')
In [43]:
image_test = gl.SFrame('image_test_data/')
In [44]:
cat_model = gl.nearest_neighbors.create(cat_image_train,
features = ['deep_features'],
label = 'id')
In [45]:
auto_model = gl.nearest_neighbors.create(auto_image_train,
features = ['deep_features'],
label = 'id')
In [46]:
bird_model = gl.nearest_neighbors.create(bird_image_train,
features = ['deep_features'],
label = 'id')
In [57]:
query_result_cat = cat_model.query(image_test[0:1])
cat_image_train.filter_by(query_result_cat['reference_label'], 'id')['image'].show()
In [134]:
query_result_dog = dog_model.query(image_test[0:1])
dog_image_train.filter_by(query_result_dog['reference_label'], 'id')['image'].show()
In [ ]:
In [63]:
## mean to
query_result_cat['distance'].mean()
Out[63]:
In [135]:
query_result_dog['distance'].mean()
#query_result_dog
Out[135]:
In [ ]:
In [65]:
cat_test_image = image_test[image_test['label']=='cat']
In [66]:
dog_test_image = image_test[image_test['label']=='dog']
In [72]:
auto_test_image = image_test[image_test['label']=='automobile']
bird_test_image = image_test[image_test['label']=='bird']
In [78]:
dog_cat_neighbors = cat_model.query(dog_test_image, k=1)
dog_cat_neighbors
Out[78]:
In [80]:
dog_auto_neighbors = auto_model.query(dog_test_image, k=1)
In [81]:
dog_bird_neighbors = bird_model.query(dog_test_image, k=1)
In [83]:
dog_dog_neighbors = dog_model.query(dog_test_image, k=1)
In [84]:
dog_distances = gl.SFrame({'dog_cat':dog_cat_neighbors['distance'],
'dog_auto': dog_auto_neighbors['distance'],
'dog_bird':dog_bird_neighbors['distance'],
'dog_dog':dog_dog_neighbors['distance']})
In [85]:
dog_distances.head()
Out[85]:
In [125]:
def is_dog_correct(row):
if row['dog_auto']< row['dog_dog']:
return 0
elif row['dog_bird']< row['dog_dog']:
return 0
elif row['dog_cat']< row['dog_dog']:
return 0
else:
return 1
In [ ]:
In [129]:
print dog_distances.apply(is_dog_correct).sum()
print len(dog_distances.apply(is_dog_correct))
In [133]:
accuracy = 678/1000.0
print "Accuracy ", accuracy
In [ ]: